package leetcode.leetcode100;

import java.util.Stack;

public class isValid {
    public static void main(String[] args) {
        String s = "([]){";
        System.out.println(process(s));
    }

    public static boolean process(String s){
        Stack<Character> strings = new Stack<Character>();
        if (s == null || s.length()<2){
            return false;
        }
        boolean flag = false;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '['){
                strings.add(s.charAt(i));
                continue;
            }else if (s.charAt(i) == ')' ){
                if (strings.isEmpty()){
                    return false;
                }
                char cur = strings.pop();
                if (cur == '('){
                    flag = true;
                }else{
                    return false;
                }
            }else if (s.charAt(i) == '}' ){
                if (strings.isEmpty()){
                    return false;
                }
                char cur = strings.pop();
                if (cur == '{'){
                    flag = true;
                }else{
                  return false;
                }
            }else if (s.charAt(i) == ']' ){
                if (strings.isEmpty()){
                    return false;
                }
                char cur = strings.pop();
                if (cur == '['){
                    flag = true;
                }else{
                    return false;
                }
            }
        }
        if (!strings.isEmpty()){
            return false;
        }
        return flag;
    }
}
